import React, { Component } from 'react';

import Unit from '../../unit';
import { Pagination } from 'antd';

class View extends Component {
    constructor(props){
        super(props);
        this.state = {
            listData:[],
            loading:true,
            pageCfg:{
                page_number:sessionStorage.getItem('currentPaget')||1,
                page_size:20,
            },
            setPage:{
                currentPage:1,
                totalPage:10
            }
        }
        this.getPage = this.getPage.bind(this);
    }
    getPage(page,size=10){
        sessionStorage.setItem('currentPaget', encodeURIComponent(page));
        const {pageCfg} = this.state;
        pageCfg.page_number = page;
        pageCfg.page_size = size;
        Unit.getApi2('home/mediareports',pageCfg,{}).then((res)=>{
            this.setState({
                listData:res.data.data,
                loading:false,
                setPage:{
                    currentPage:page,
                    totalPage:res.data.total,
                }
            })
        })
    }
    componentDidMount(){
        const {pageCfg} = this.state;
        setTimeout(()=>{
            this.getPage(pageCfg.page_number);
        },4000)
    }
    listFn(){
        const {listData} = this.state;
        const lists = listData.map((val,index)=>{
            return(
                <li key={val.id}>
                    <b>{val.id}</b>
                    <a href={val.jump_url} title={val.main_title}>{val.main_title}</a>
                    <span>{val.source}</span>
                    <span>{val.updatedAt}</span>
                </li>
            )
        })
        return lists;
    }
    render(){
        const {listData,loading,setPage} = this.state;
        return(
            <React.Fragment>
                {
                    loading ?
                    (<div className="loading_box"><div className="loading"></div></div>) :
                    (listData.length!==0 ? this.listFn() : <div className="no_result">小主~,此处空的可怜~</div>)
                }
                {
                    listData.length!==0?
                    <Pagination onChange={this.getPage} defaultCurrent={setPage.currentPage} total={setPage.totalPage} />:''
                }
            </React.Fragment>
        )
    }
}
export default View;
